<!DOCTYPE>
<html>
<meta charset="utf-8"/>
<title>异步绑定用户信息通过Websocket,Gateway绑定是有问题的</title>
<script src="http://cdn.bootcss.com/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    var $URL = "__APP__/Home/GatewayWorker/demoBindUidAjax";
    var userData;
    // 与GatewayWorker建立websocket连接，域名和端口改为你实际的域名端口
    ws = new WebSocket("ws://120.26.220.223:8384");//对应这个$gateway_ws = new Gateway("Websocket://0.0.0.0:8384");
    // 服务端主动推送消息时会触发这里的onmessage
    ws.onmessage = function(e){
        // json数据转换成js对象
        var data = eval("("+e.data+")");
        var init_type = data.init_type || '';
        switch(init_type){
            // Events.php中返回的init类型的消息，将client_id发给后台进行uid绑定
            case 'init':
                // 利用jquery发起ajax请求，将client_id发给后端进行uid绑定
                var $requestData = {client_id: data.clientId};
                var success = function(response)
                {
                    userData = {
                        user_id:response.user_id,
                        type:response.type
                    };
                    ws.send(JSON.stringify(userData));
                    console.warn(userData);
                }
                $.post($URL,$requestData,success,'json');
                break;
            default :
                console.log(e.data);
        }
    };

</script>

<h2>WebSocketDemo</h2>
数据：<input type="text" style="height: 50px; width: 100%;" name="data" id="data"><p></p>
<button id="submit" onclick="sub()" >提交</button> <p></p>
<!--<button id="submit" onclick="forExample()" >JSON数据</button> <p></p>-->

<div id="output"></div>
<div id="message"></div>
</html>
